require('./PagePurchase.styl');

const reactMixin = require('react-mixin');
const Actions = require('./actions');
const Store = require('./store');
const { Group, TextField, SelectField, DatetimeField, Button, Boxs, Toast, TextareaField } = SaltUI;
const { HBox, Box } = Boxs;
import Addpersonfield from 'components/addpersonfield'
import { mergeArray } from 'clientConfig/util/queryurlfield';
const Fileselect = require('components/fileselect')

class Purchase extends React.Component {

    constructor(props) {
        super(props);
        this.state = {
            upfileName: []
        };
        this.data = [
            { label: "年度", name: "annual1" },
            { label: "专项", name: "special" },
            { label: "项目名称", name: "proName" },
            { label: "项目编号", name: "proNum" },
            { label: "预算金额", name: "budget" },
            { label: "中标单位", name: "builder" },
            { label: "中标单位信息", name: "winbid_info" },
        ];//            { label: "投标单位信息", name: "bid_info" },
        if (props.pagedata) {
            let nodelists = props.pagedata.node_info;
            for (let i = 0; i < this.data.length; i++) {
                if (this.data[i].name) {
                    if (i == 0) {
                        this.state["annual"] = new Date(nodelists[i].value).getTime()//年度必须为this.data的第一个
                        this.state["annual1"] = nodelists[i].value//年度必须为this.data的第一个
                    } else {
                        this.state[this.data[i].name] = nodelists[i].value;
                    }
                }
            }
            this.state.upfileName = JSON.parse(nodelists[nodelists.length - 1].value)//附件信息
            this.state.proName = props.pagedata.proName
            this.state.proNum = props.pagedata.proNum
            this.state.edit = props.edit;
        }
    }
    handleTextChange(name, newValue) {
        if (name == "proName") {
            let obj = this.state.proNameAll[newValue.value];
            this.setState({
                proNum: this.state.proNumAll[newValue.value],
                folder_id: obj["folder_id"],
                annual: obj["annual"],
                annual1: new Date(obj["annual"]).toLocaleDateString().split('/')[0],
                special: obj["special"],
                budget: obj["budget"],
                path_name: ''
            })
        }
        this.setState({
            [name]: newValue
        });
    }

    handleTimeChange(name, newValue) {
        this.setState({
            [name]: newValue,
            [name + '1']: new Date(newValue).toLocaleDateString(),
        });
    }
    //提交按钮
    handleClick() {
        if (!this.state.proName) {
            Toast.show({
                type: 'error',
                content: `项目名称不能为空`
            });
            return;
        } else if (!this.state.proNum) {
            Toast.show({
                type: 'error',
                content: `项目编号不能为空`
            });
            return;
        } else if (!this.state.annual) {
            Toast.show({
                type: 'error',
                content: `年度不能为空`
            });
            return;
        } else if (!this.state.special) {
            Toast.show({
                type: 'error',
                content: `专项不能为空`
            });
            return;
        } else if (!this.state.budget) {
            Toast.show({
                type: 'error',
                content: `预算不能为空`
            });
            return;
        } else if (!this.state.builder) {
            Toast.show({
                type: 'error',
                content: `中标单位不能为空`
            });
            return;
        } else if (!this.state.winbid_info) {
            Toast.show({
                type: 'error',
                content: `中标单位信息不能为空`
            });
            return;
        } else if (!this.state.path_name) {
            Toast.show({
                type: 'error',
                content: `文件存储区域不能为空`
            });
            return;
        } else if (!this.refs.fileselect.fileData || this.refs.fileselect.fileData.length == 0) {
            Toast.show({
                type: 'error',
                content: `附件不能为空`
            });
            return;
        } else if (!this.state['approvers']) {
            Toast.show({
                type: 'error',
                content: `审批人不能为空`
            });
            return;
        }
        if (this.props.pagedata) {
            Actions.submit({ node: this.data, data: this.state, upfileName: this.refs.fileselect.fileData, process_id: this.process_id, edit: this.state.edit, pagedata: this.props.pagedata });
        } else {
            Actions.submit({ node: this.data, data: this.state, upfileName: this.refs.fileselect.fileData, process_id: this.process_id });
        }
    }

    handleAddpersonCb(names) {
        this.setState({ approvers: names })
    }
    handleCCpersonCb(names) {
        this.setState({ copyers: names })
    }
    handleAddpersonUnionid(unionids) {
        this.setState({
            approvers_id: unionids
        })
    }
    setPathName(path_name) {
        this.setState({ path_name })
    }
    handlecsrUnionid(unionids) {
        this.setState({
            cc_list_id: unionids
        })
    }


    render() {
        let t = this;
        return (
            <div className="purchase myform">
                <Group.List >
                    <SelectField
                        label="项目名称"
                        placeholder="请选择"
                        options={t.state.proNameAll}
                        value={t.state.proName}
                        onSelect={t.handleTextChange.bind(t, 'proName')}
                        readOnly={t.props.edit}
                    />
                    <TextField
                        label="项目编号"
                        value={t.state.proNum}
                        onChange={t.handleTextChange.bind(t, 'proNum')}
                        readOnly={true}
                    />
                </Group.List>
                <Group.List >
                    <DatetimeField
                        label="年度"
                        placeholder="请选择"
                        columns={['年']}
                        className="time"
                        value={this.state.annual}
                        onSelect={this.handleTimeChange.bind(t, 'annual')}
                        readOnly={true}
                    />
                </Group.List>

                <Group.List >
                    <TextField
                        label="专项"
                        placeholder="请输入专项名称"
                        value={t.state.special}
                        onChange={t.handleTextChange.bind(t, 'special')}
                        readOnly={true}
                    />
                </Group.List>
                <Group.List >
                    <TextField
                        label="预算金额"
                        placeholder="请输入预算金额"
                        value={t.state.budget}
                        onChange={t.handleTextChange.bind(t, 'budget')}
                        readOnly={true}
                    />
                </Group.List>
                <Group.List >
                    <TextField
                        label="中标单位"
                        placeholder="请输入中标单位"
                        value={t.state.builder}
                        onChange={t.handleTextChange.bind(t, 'builder')}
                        readOnly={t.props.edit}
                    />
                    {/* <TextareaField
                        minRows={4} maxRows={10}
                        placeholder="投标单位信息"
                        value={t.state.bid_info}
                        readOnly={this.props.edit}
                        onChange={t.handleTextChange.bind(t, 'bid_info')}
                    /> */}
                    <TextareaField
                        minRows={4} maxRows={10}
                        placeholder="中标单位信息"
                        value={t.state.winbid_info}
                        readOnly={this.props.edit}
                        onChange={t.handleTextChange.bind(t, 'winbid_info')}
                    />
                </Group.List>
                <Fileselect
                    ref='fileselect'
                    version={true}
                    upfileName={this.state.upfileName}
                    proName={this.state.proName}
                    edit={this.props.edit}
                    folder_id={this.state.folder_id}
                    process_folder={true}//是不是需要审批通过才存储文件
                    pathdata={this.state.pathdata}
                    path_name={this.state.path_name}
                    setPathName={this.setPathName.bind(this)}
                    message="tender-tenderpurchase"
                />
                <Group.List>
                    <Addpersonfield
                        label="发送给：甲方"
                        cb={t.handleAddpersonCb.bind(t)}
                        union={t.handleAddpersonUnionid.bind(t)}
                        approvers={t.state.approvers}
                        approvers_id={t.state.approvers_id}
                        maxLength="10" />
                </Group.List>
                <Group.List>
                    <Addpersonfield
                        label="抄送人"
                        cb={t.handleCCpersonCb.bind(t)}
                        union={t.handlecsrUnionid.bind(t)}
                        approvers={t.state.cc_list}
                        approvers_id={t.state.cc_list_id}
                        maxLength="10"
                    />
                </Group.List>
                <Button type="primary" className="submitBtn" onClick={t.handleClick.bind(t)}>提交</Button>
            </div>
        );
    }

    componentWillMount() {
    }

    componentDidMount() {
        Actions.fetch()
    }

    componentWillReceiveProps(nextProps) {
        if (nextProps.pagedata.approvers && nextProps.pagedata.approvers instanceof Array)
            this.setState({
                approvers: nextProps.pagedata.approvers,
                approvers_id: nextProps.pagedata.approvers_id,
            });
        else if (nextProps.pagedata.approvers && typeof nextProps.pagedata.approvers == 'string')
            this.setState({
                approvers: [nextProps.pagedata.approvers],
                approvers_id: [nextProps.pagedata.approvers_id]
            });
        if (nextProps.pagedata.cc_list && nextProps.pagedata.cc_list instanceof Array)
            this.setState({
                cc_list: nextProps.pagedata.cc_list,
                cc_list_id: nextProps.pagedata.cc_list_id
            });
        else if (nextProps.pagedata.cc_list && typeof nextProps.pagedata.cc_list == 'string')
            this.setState({
                cc_list: [nextProps.pagedata.cc_list],
                cc_list_id: [nextProps.pagedata.cc_list_id]
            })
    }

    shouldComponentUpdate(nextProps, nextState) {
        return true;
    }

    componentWillUpdate(nextProps, nextState) {
    }

    componentDidUpdate(prevProps, prevState) {
    }

    componentWillUnmount() {
    }
}

reactMixin.onClass(Purchase, Reflux.connect(Store));

module.exports = Purchase;
